<template>
	<view class="context">
		<view class="title">账号密码登录</view>
		<!-- 表单发送 -->
		<form @submit.prevent="submitForm">
			<view class="form-box">
				<!-- 账号输入框 -->
				<input type="text" placeholder="请输入手机号码" v-model="username" />
				<!-- 密码输入框 -->
				<input type="password" placeholder="请输入密码" v-model="password" />
				<!-- 登录按钮 -->
				<button class="sub" form-type="submit">登录</button>
				<!-- 其他功能 -->
				<view class="other">
					<text>账号注册</text>
					<text>忘记密码?</text>
				</view>
			</view>
		</form>
	</view>
</template>

<script>
	import {
		request
	} from '../../utils/request.js';
	export default {
		data() {
			return {
				username: '',
				password: ''
			}
		},

		methods: {
			//提交账号密码
			submitForm() {
				//判空
				if (this.username === '' && this.password === '') {
					console.log('信息为空');
					return;
				}
				//请求
				let res = request.userlogin({
					username: this.username,
					password: this.password,
					logintype: 'phone'
				});
				//判断结果
				res.then((res) => {
					if (res.code === 1) {
						//保存 data信息
						uni.setStorageSync('Token',res.data.token);
						uni.setStorageSync('UserInfo', res.data);
						//跳转页面
						this.jumpMePage();
					} else {
						uni.showToast({
                            title: '账号或密码错误',
                            icon:'error'
                        });
					}
				});
			},
			jumpMePage() {
				uni.switchTab({
					url: '/pages/me/me'
				});
			}
		}
	}
</script>

<style lang="scss">
	@import "login.css";
</style>